﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.ComponentModel;
using ClassLibrary.DAO;

namespace ClassLibrary.SMTP
{
    public class SMTPMail
    {
        public void SendEmail(MailProperties mailProperties, bool isBodyHtml, bool IsAsynchronous)
        {
            MailMessage objMail = new MailMessage();
            SmtpClient smtpClient = new SmtpClient();
            MailAddress mailAddress = new MailAddress(mailProperties.SMTPFrom, mailProperties.SMTPDisplayName);
            objMail.From = mailAddress;
            objMail.To.Add(mailProperties.SMTPTo);
            objMail.Subject = mailProperties.SMTPSubject;
            objMail.IsBodyHtml = isBodyHtml;
            objMail.Body = mailProperties.SMTPBody;
            objMail.BodyEncoding = Encoding.UTF8;
            smtpClient.Host = mailProperties.SMTPHost;
            smtpClient.Port = mailProperties.SMTPPort;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

            NetworkCredential networkCre = new NetworkCredential();
            networkCre.UserName = mailProperties.SMTPUsername;
            networkCre.Password = mailProperties.SMTPPassword;
            smtpClient.Credentials = networkCre;

            if (mailProperties.SMTPAttachedFiles.Trim() != "")
            {
                string allAttachFile = mailProperties.SMTPAttachedFiles.Trim();
                if (allAttachFile.EndsWith(";"))
                    allAttachFile = allAttachFile.Remove(allAttachFile.LastIndexOf(";"));
                string[] arrFileAttaches = allAttachFile.Split(';');
                for (int i = 0; i < arrFileAttaches.Length; i++)
                {
                    Attachment objAttach = new Attachment(arrFileAttaches[i].ToString().Trim());
                    if (objAttach != null)
                        objMail.Attachments.Add(objAttach);
                }
            }
            if (smtpClient.Host != null)
            {
                try
                {
                    if (!IsAsynchronous)
                        smtpClient.Send(objMail);
                    else
                    {
                        smtpClient.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);
                        string userState = "message sent";
                        smtpClient.SendAsync(objMail, userState);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    mailAddress = null;
                    objMail = null;
                    smtpClient = null;
                }
            }
        }

        public void SendEmail(MailProperties mailProperties, bool isBodyHtml, bool IsAsynchronous, string RelayFolderLocation)
        {
            MailMessage objMail = new MailMessage();
            SmtpClient smtpClient = new SmtpClient();
            MailAddress mailAddress = new MailAddress(mailProperties.SMTPFrom, mailProperties.SMTPDisplayName);
            objMail.From = mailAddress;
            objMail.To.Add(mailProperties.SMTPTo);
            objMail.Subject = mailProperties.SMTPSubject;
            objMail.IsBodyHtml = isBodyHtml;
            objMail.Body = mailProperties.SMTPBody;
            objMail.BodyEncoding = Encoding.UTF8;
            smtpClient.Host = mailProperties.SMTPHost;
            smtpClient.Port = mailProperties.SMTPPort;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

            NetworkCredential networkCre = new NetworkCredential();
            networkCre.UserName = mailProperties.SMTPUsername;
            networkCre.Password = mailProperties.SMTPPassword;
            smtpClient.Credentials = networkCre;

            if (mailProperties.SMTPAttachedFiles.Trim() != "")
            {
                string allAttachFile = mailProperties.SMTPAttachedFiles.Trim();
                if (allAttachFile.EndsWith(";"))
                    allAttachFile = allAttachFile.Remove(allAttachFile.LastIndexOf(";"));
                string[] arrFileAttaches = allAttachFile.Split(';');
                for (int i = 0; i < arrFileAttaches.Length; i++)
                {
                    Attachment objAttach = new Attachment(arrFileAttaches[i].ToString().Trim());
                    if (objAttach != null)
                        objMail.Attachments.Add(objAttach);
                }
            }
            if (smtpClient.Host != null)
            {
                try
                {
                    if (!IsAsynchronous)
                        smtpClient.Send(objMail);
                    else
                    {
                        smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                        smtpClient.PickupDirectoryLocation = RelayFolderLocation;
                        smtpClient.Send(objMail);

                        //if (RelayFolderLocation != String.Empty)
                        //{
                        //    if (!Directory.Exists(RelayFolderLocation))
                        //        Directory.CreateDirectory(RelayFolderLocation);
                        //    string strDelimiter = ",";
                        //    string strFileName = objMail.From + strDelimiter +
                        //                         objMail.Subject + strDelimiter +
                        //                         objMail.To + ".txt";
                        //    File.WriteAllText(RelayFolderLocation + strFileName, objMail.Body);
                        //}
                        //smtpClient.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);
                        //string userState = "message sent";
                        //object userState = objMail;
                        //smtpClient.SendAsync(objMail, userState);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    mailAddress = null;
                    objMail = null;
                    smtpClient = null;
                }
            }
        }

        private void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
            }
            if (e.Error != null)
            {
            }
            MailMessage msg = (MailMessage)e.UserState;
            msg.Dispose();
        }
    }
}
